package com.jinke.uang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinke.uang.R;
import com.jinke.uang.bean.MoneyProductData;
import com.jinke.uang.utils.StringUtils;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页产品的适配器
 */
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
    private Context context;
    private List<MoneyProductData> data;
    private OnItemClickListener onItemCLickListener;
    private int mSelectItem = 0;

    public ProductAdapter(Context context, List<MoneyProductData> data) {
        this.context = context;
        this.data = data;
    }

    public List<MoneyProductData> getData() {
        return data;
    }

    public void setData(List<MoneyProductData> data) {
        this.data = data;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_progress_layout, parent,
                false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ProductViewHolder holder, final int position) {
        holder.tvContent.setText(String.format("Rp: %s", StringUtils.getDotMoneyStr(data.get
                (position).getProdRange() + "")));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    mSelectItem = position;
                    notifyDataSetChanged();
                    if (onItemCLickListener != null) {
                        onItemCLickListener.onClick(holder.itemView, holder.getLayoutPosition());
                    }
            }
        });
    //你操作我刚给适配器写好
        if ("1".equals(data.get(position).getRangeStatus())) {
            if (mSelectItem >= 0 && mSelectItem == position) {
                holder.itemView.setBackgroundResource(R.drawable.home_product_click_select_bg);
                holder.tvContent.setTextColor(context.getResources().getColor(R.color.colorWhite));
            } else {
                mSelectItem = position;
                holder.itemView.setBackgroundResource(R.drawable.home_product_click_noselect_bg);
                holder.tvContent.setTextColor(context.getResources().getColor(R.color.logo_color));
             /*   holder.itemView.setBackgroundResource(R.drawable.home_product_click_noselect_bg);
                holder.tvContent.setTextColor(context.getResources().getColor(R.color.home_product_noselect));*/
            }
        }else {
            holder.itemView.setBackgroundResource(R.drawable.home_product_noclick_bg);
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.home_product_noclick));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ProductViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_content)
        TextView tvContent;

        public ProductViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemCLickListener) {
        this.onItemCLickListener = onItemCLickListener;
    }

    public int getSelectItem() {
        return mSelectItem;
    }
    public void setSelectItem(int selectItem) {
        mSelectItem = selectItem;
    }
}
